package Leo.controller;

import Leo.entity.User;
import org.springframework.http.HttpRequest;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.*;


@RestController
public class SimpleController {
    static double total = 0;
    static double timeout = 0;

    @RequestMapping(value = "/xmlUser", //rest路径
            method = RequestMethod.POST, //指定访问方法
            consumes = MediaType.APPLICATION_XML_VALUE,  //指定处理请求的提交内容类型（Content-Type）
            produces = MediaType.APPLICATION_XML_VALUE  //指定返回值类型，不但可以设置返回值类型还可以设定返回值的字符编码
//                    produces = "MediaType.APPLICATION_XML_VALUE;charset=utf-8"
    )
    public String xmlUser(@RequestBody User user) {
        System.out.println("xml receive user: " + user.getUserName());
        String result = "<result><message>received success! " + user.getUserName() + "</message></result>";
        return result;
    }

    @RequestMapping(value = "/jsonUser", //rest路径
            method = RequestMethod.POST, //指定访问方法
            consumes = MediaType.APPLICATION_JSON_VALUE  //指定处理请求的提交内容类型（Content-Type）
    )
    public User jsonUser(@RequestBody User user) {
        System.out.println("json receive user: " + user.getUserName());
        user.setUserName("阿十六");
        return user;
    }

    @RequestMapping(value = "/jsonUserToString", //rest路径
            method = RequestMethod.POST, //指定访问方法
            consumes = MediaType.APPLICATION_JSON_VALUE  //指定处理请求的提交内容类型（Content-Type）
    )
    public String jsonUserToString(@RequestBody User user) {
        System.out.println("json receive user: " + user.getUserName());
        user.setUserName("北河真人");
        return user.getUserName();
    }

    @RequestMapping(value = "/stringUser", //rest路径
            method = RequestMethod.POST //指定访问方法
//            ,consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE  //指定处理请求的提交内容类型（Content-Type）
    )
    public String stringUser(String user) {
        System.out.println("json receive user: " + user);

        return user + " ： 葱娘";
    }


//    Hystrix测试例子
    @RequestMapping("/hello")
    public String hello(@RequestParam("time") Integer time) throws InterruptedException {
        Random random = new Random();
        int randomInt = random.nextInt(2000);
        total++;
        if(time != null && randomInt >= time){
            timeout++;
        }
        System.out.println("触发hello,线程随机睡眠时长：" + randomInt + ",超时百分比：" + (timeout/total*100));
        Thread.sleep(randomInt);
        return "hello:" + randomInt;
    }

    @RequestMapping("/errHello")
    public String errHello() throws InterruptedException {
        Thread.sleep(3000);
        return "error ,hello";
    }

    /**
     * 获取单个user
     * @param userId
     * @return
     */
    @RequestMapping("/getUser")
    public User getUser(@RequestParam("userId")String userId){
        User user = new User();
        user.setUserName(randomUserName());
        Random random = new Random();
        user.setUserId(userId);
        return user;
    }

    /**
     * 批量获取user
     * @return
     */
    @RequestMapping(value = "/getUsers",method = RequestMethod.POST, //指定访问方法
            consumes = MediaType.TEXT_HTML_VALUE ) //指定处理请求的提交内容类型（Content-Type）)
    public Map<String,User> getUsers(@RequestParam("userId")String userId){
//        String userId = "343,222";
        HashMap<String, User> userHashMap = new HashMap<String, User>();
        String[] split = userId.split(",");
        for (String s : split) {
            if(s != null && !s.equals(""))
                userHashMap.put(s,getUser(s));
        }
        return userHashMap;
    }

    private String randomUserName(){
        ArrayList<String> nameList = new ArrayList<String>();
        nameList.add("苏氏阿七");
        nameList.add("白真君");
        nameList.add("等待与诺言仙子");
        nameList.add("东方六仙子");
        nameList.add("荔枝仙子");
        nameList.add("道子");
        nameList.add("赤霄子");
        nameList.add("铜卦天仙");
        nameList.add("北河散人");
        nameList.add("苏氏阿十六");
        nameList.add("雨柔子");
        nameList.add("黄山真君");
        nameList.add("七修玄圣");
        nameList.add("葱娘");
        Random random = new Random();
        int i = random.nextInt(nameList.size());
        return nameList.get(i);


    }
}
